package com.ljfiction.book.activitys;


import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.ljfiction.book.Dialog.ToShareDialog;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserBean;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.manage.CodeManager;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class InviteActivity extends BaseActivity {

    private String bookName, bookId, bookImage;
    private ToShareDialog toShareDialog;
    private boolean isshare = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite);
        ButterKnife.bind(this);
        bookName = getIntent().getStringExtra("bookName");
        bookId = getIntent().getStringExtra("bookId");
        bookImage = getIntent().getStringExtra("bookImage");


    }

    @OnClick({R.id.invite_goback, R.id.invite_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.invite_goback:
                if (isshare) {
                    setResult(CodeManager.invite_friends);
                }
                goBack();
                break;
            case R.id.invite_share:
                if (toShareDialog == null) {
                    toShareDialog = new ToShareDialog(InviteActivity.this, bookName, bookId, bookImage, String.valueOf(UserManageBean.userBean.getUserId()), 1);
                    toShareDialog.setListener(new ToShareDialog.Listener() {
                        @Override
                        public void toRefreshi() {
                            OkHttpUtils.get(HttpManager.IP + "/SharingOperationRecord")
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onSuccess(String s, Call call, Response response) {
                                            try {
                                                JSONObject jsonObject=new JSONObject(s);
                                                if (jsonObject.getInt("returnCode")==200){
                                                    backgroundRiches();
                                                }
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }

                                        }
                                    });

                        }
                    });
                }
                toShareDialog.show();
                break;
        }
    }

    private void backgroundRiches() {
        OkHttpUtils.post(HttpManager.IP + "/person-info")

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                UserManageBean.userBean = gson.fromJson(jsonObject.getString("data"), UserBean.class);
                                UserManageBean.setIsLogin(true);
                                isshare = true;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

}
